﻿using Microsoft.AspNetCore.Http;
using System;
using ZoLone.Zo.Http;

namespace Zo.Http
{
    public class HttpCookieHelper
    {

        private static HttpContext httpContext => HttpAccessorService._HttpContext;


        #region Cookie操作
        /// <summary>
        /// 写入Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expires">过期时长</param>
        public static void SetCookie(string key, string value, int? expires = null)
        {
            if(expires==null)
            {
                httpContext.Response.Cookies.Append(key,value);
            }
            else
            {
                httpContext.Response.Cookies.Append(key, value, new CookieOptions()
                {
                    Expires = DateTime.Now.AddMinutes(Convert.ToDouble(expires))
                }) ; 
            }
        }

        /// <summary>
        /// 读Cookie值
        /// </summary>
        /// <param name="strName">cookies名称</param>
        /// <returns>返回的cookies</returns>
        public static string GetCookie(string key)
        {
            var obj = httpContext.Request.Cookies[key];
            if (obj == null) return null;
            return obj;
        }

        /// <summary>
        /// 删除Cookie对象
        /// </summary>
        /// <param name="CookiesName">Cookie对象名称</param>
        public static void RemoveCookie(string key)
        {
            httpContext.Response.Cookies.Delete(key);
        }
        #endregion Cookie操作
    }
}
